<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>SpringBoot系列—全局配置（四） | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="&amp;emsp;&amp;emsp;用IDEA可以直接创建SpringBoot项目，创建Moudle的时候，在选Maven的地方，选择Sping Initializr即可，很简单。前面讲过了IDEA生成Maven项目，并且嵌入SpringBoot，直接创建Spring Boot项目就是IDEA直接帮我们依赖，并且生成Application和properties文件。其它，也没啥大的区别，就不多说了。">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot系列—全局配置（四）">
<meta property="og:url" content="https://haichenyi.com/2019/08/23/SpringBoot系列—全局配置（四）/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="&amp;emsp;&amp;emsp;用IDEA可以直接创建SpringBoot项目，创建Moudle的时候，在选Maven的地方，选择Sping Initializr即可，很简单。前面讲过了IDEA生成Maven项目，并且嵌入SpringBoot，直接创建Spring Boot项目就是IDEA直接帮我们依赖，并且生成Application和properties文件。其它，也没啥大的区别，就不多说了。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2019-08-23/properties修改端口号.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2019-08-23/yml修改端口号.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2019-08-23/局部配置文件.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2019-08-23/profile-properties.png">
<meta property="og:updated_time" content="2019-09-18T09:10:13.925Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SpringBoot系列—全局配置（四）">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;用IDEA可以直接创建SpringBoot项目，创建Moudle的时候，在选Maven的地方，选择Sping Initializr即可，很简单。前面讲过了IDEA生成Maven项目，并且嵌入SpringBoot，直接创建Spring Boot项目就是IDEA直接帮我们依赖，并且生成Application和properties文件。其它，也没啥大的区别，就不多说了。">
<meta name="twitter:image" content="https://haichenyi.com/uploads/article/2019-08-23/properties修改端口号.png">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-SpringBoot系列—全局配置（四）" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      SpringBoot系列—全局配置（四）
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2019/08/23/SpringBoot系列—全局配置（四）/" class="article-date">
  <time datetime="2019-08-23T07:18:39.000Z" itemprop="datePublished">2019-08-23</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <p>&emsp;&emsp;用IDEA可以直接创建SpringBoot项目，创建Moudle的时候，在选Maven的地方，选择Sping Initializr即可，很简单。前面讲过了IDEA生成Maven项目，并且嵌入SpringBoot，直接创建Spring Boot项目就是IDEA直接帮我们依赖，并且生成Application和properties文件。其它，也没啥大的区别，就不多说了。</p>
<a id="more"></a>
<p>&emsp;&emsp;本篇要讲的是SpringBoot的配置文件，分为两种：</p>
<ul>
<li>properties</li>
<li>yml</li>
</ul>
<p>&emsp;&emsp;之前，搜SpringBoot的配置文件怎么写的时候，看到网上很多博客都有文件内容，但是有两种写法，就是这两种配置文件的不同写法。</p>
<p>&emsp;&emsp;举个栗子，我现在要修改服务器启动的端口号，用properties怎么修改呢？很简单：</p>
<p><img src="/uploads/article/2019-08-23/properties修改端口号.png" alt="properties修改端口号.png"></p>
<p>&emsp;&emsp;如上图所示，是不是很简单？</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">server.port=8081</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;用yml怎么修改呢？也不难：</p>
<p><img src="/uploads/article/2019-08-23/yml修改端口号.png" alt="yml修改端口号.png"></p>
<p>&emsp;&emsp;如上图所示：也不难</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server:</span><br><span class="line">  port: 808</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;<strong>看到了上面两个图片，应该也已经发现了，两个配置文件存放位置都在resource目录下面。或者在类路径的”/config”路径下</strong></p>
<p>&emsp;&emsp;这两种写法要怎么写呢？</p>
<p><strong>properties：</strong> 等号连接，右边是值</p>
<p><strong>yml：key:</strong></p>
<ul>
<li>value 表示一对键值对（冒号后面必须要有空格）</li>
<li>使用空格缩进表示层级关系</li>
<li>左侧缩进的空格数目不重要，只要同一层级的元素左侧对齐即可</li>
<li>key 与 value 大小写敏感</li>
</ul>
<p>PS: yml不管是存map，还是list，都要记得<strong>key后面的冒号一定要跟空格，再写值</strong></p>
<p>&emsp;&emsp;举个栗子，我们定义一个bean类，在配置文件里面赋值，在项目里面取出来用。比方说：我们项目里面定义一个Userbean类。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot.pojo;</span><br><span class="line"></span><br><span class="line">import lombok.Data;</span><br><span class="line">import org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line">import org.springframework.context.annotation.PropertySource;</span><br><span class="line">import org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line">@Data</span><br><span class="line">@Component</span><br><span class="line">@ConfigurationProperties(prefix = &quot;user&quot;)</span><br><span class="line">public class User &#123;</span><br><span class="line">    private String username;</span><br><span class="line">    private String nickname;</span><br><span class="line">    private String password;</span><br><span class="line">    private Integer age;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;这里有几点需要注意：</p>
<ol>
<li>这里的bean类，必须要有写set/get方法，不然赋不了值，我这里没写是因为我用的@Data这个注解，它默认帮我们创建了set/get方法</li>
<li>@ConfigurationProperties注解就是 告诉SpringBoot将配置文件中对应属性的值，映射到这个组件类中,进行一 一绑定，prefix = “user”的作用就是配置文件中的前缀名，哪个前缀与下面的所有属性进行一一映射</li>
<li>@Component 注解就是将当前组件也就是这个bean类作为SpringBoot中的一个组件，才能使用容器提供的</li>
</ol>
<p>然后就是在配置文件中赋值，properties或者是yml，都可以：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">properties中：</span><br><span class="line"></span><br><span class="line"> server.port=8081</span><br><span class="line">user.username=s19734682s</span><br><span class="line">user.age=20</span><br><span class="line">user.password=123456</span><br><span class="line">user.nickname=林汐痕</span><br><span class="line"></span><br><span class="line">yml中：</span><br><span class="line"></span><br><span class="line">server:</span><br><span class="line">  port: 8088</span><br><span class="line">  </span><br><span class="line">//这个user就是上面说的配置文件中的前缀名</span><br><span class="line">user:</span><br><span class="line">  nickname: 海晨忆</span><br><span class="line">  username: pk19734682</span><br><span class="line">  password: 123456</span><br><span class="line">  age: 18</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;怎么使用呢？直接在controller中用这个user就可以了</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot.controller;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.springboot.pojo.User;</span><br><span class="line">import org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line">import org.springframework.stereotype.Controller;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line">import org.springframework.web.bind.annotation.ResponseBody;</span><br><span class="line"></span><br><span class="line">@Controller</span><br><span class="line">public class SayController &#123;</span><br><span class="line">    @Autowired</span><br><span class="line">    private User user;</span><br><span class="line"></span><br><span class="line">    @ResponseBody</span><br><span class="line">    @RequestMapping(&quot;/say&quot;)</span><br><span class="line">    public String say() &#123;</span><br><span class="line">        return &quot;hello &quot; +user;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;就这样，请求这个接口就能打印值。</p>
<p>&emsp;&emsp;还有通过@Value指定值的方式</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Controller</span><br><span class="line">public class SayController &#123;</span><br><span class="line">    @Value(&quot;$&#123;user.nickname&#125;&quot;)</span><br><span class="line">    private String nickname;</span><br><span class="line"></span><br><span class="line">    @ResponseBody</span><br><span class="line">    @RequestMapping(&quot;/say&quot;)</span><br><span class="line">    public String say() &#123;</span><br><span class="line">        return &quot;hello &quot; + nickname;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<h3 id="PropertySource-加载局部配置文件"><a href="#PropertySource-加载局部配置文件" class="headerlink" title="@PropertySource 加载局部配置文件"></a>@PropertySource 加载局部配置文件</h3><p>&emsp;&emsp;什么叫加载局部配置文件呢？就是加载指定的配置文件，并不是从properties或者是yml中加载。怎么加载呢？如下图：</p>
<p><img src="/uploads/article/2019-08-23/局部配置文件.png" alt="局部配置文件.png"></p>
<p><strong>第一步，</strong> 在resources目录下创建xxx.properties/xxx.yml，这个xxx你自己命名。我这里创建的是user.properties/user.yml。记得语法不要写错了</p>
<p><strong>第二步，</strong> 在你的组件的地方加上@PropertySource注解。图上有，可以导入多个局部配置，用逗号隔开，每个局部配置的写法就是 <strong>“classpath:文件名”</strong> 之前导入全局的配置的时候加的注解也需要，不能删除。</p>
<p><strong>第三步，</strong> 之前是怎么使用的，现在还是怎么使用。</p>
<h3 id="ImportResource加载xml配置文件"><a href="#ImportResource加载xml配置文件" class="headerlink" title="@ImportResource加载xml配置文件"></a>@ImportResource加载xml配置文件</h3><p>&emsp;&emsp;Spring Boot框架并不推荐用xml加载配置文件，这个是Spring加载文件的方式。项目中如果必须要用到xml加载文件，要怎么办呢？</p>
<p>举个栗子：我要加载一个Service类到项目中。Spring Boot有注解可以直接使用，这里，我们通过xml加载。</p>
<p><strong>第一步，</strong> 创建一个service。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot.service;</span><br><span class="line"></span><br><span class="line">public class UserService &#123;</span><br><span class="line">    public void say() &#123;</span><br><span class="line">        System.out.println(&quot;xml...&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p><strong>第二步，</strong> 在resources目录下，创建spring config的xml配置文件</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt;</span><br><span class="line">    &lt;bean id=&quot;userService&quot; class=&quot;com.haichenyi.springboot.service.UserService&quot;&gt;&lt;/bean&gt;</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;如上，添加了一个bean类，两个属性，id和class。class指向刚才创建的类，id用于获取这个类。</p>
<p><strong>第三步，</strong> 在我们的引导类中加上 <strong>@ImportResource</strong> 注解</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot;</span><br><span class="line"></span><br><span class="line">import org.springframework.boot.SpringApplication;</span><br><span class="line">import org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line">import org.springframework.context.annotation.ImportResource;</span><br><span class="line"></span><br><span class="line">@ImportResource(locations = &quot;classpath:springboot01.xml&quot;)</span><br><span class="line">@SpringBootApplication</span><br><span class="line">public class SpringBootInitApplication &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        SpringApplication.run(SpringBootInitApplication.class, args);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p><strong>第四步，</strong> 通过ApplicationContext的getBean方法获取，传的参数就是在xml中定义的id。如下：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.springboot.pojo.User;</span><br><span class="line">import com.haichenyi.springboot.service.UserService;</span><br><span class="line">import org.junit.Test;</span><br><span class="line">import org.junit.runner.RunWith;</span><br><span class="line">import org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line">import org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line">import org.springframework.context.ApplicationContext;</span><br><span class="line">import org.springframework.test.context.junit4.SpringRunner;</span><br><span class="line"></span><br><span class="line">@RunWith(SpringRunner.class)</span><br><span class="line">@SpringBootTest</span><br><span class="line">public class SpringBootInitApplicationTests &#123;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    User user;</span><br><span class="line"></span><br><span class="line">    @Test</span><br><span class="line">    public void contextLoads() &#123;</span><br><span class="line">        System.out.println(user);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    ApplicationContext context;</span><br><span class="line"></span><br><span class="line">    @Test</span><br><span class="line">    public void textXml() &#123;</span><br><span class="line">        UserService userService = (UserService) context.getBean(&quot;userService&quot;);</span><br><span class="line">        userService.say();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<h3 id="自定义配置类向容器中注入组件-SpringBoot推荐"><a href="#自定义配置类向容器中注入组件-SpringBoot推荐" class="headerlink" title="自定义配置类向容器中注入组件(SpringBoot推荐)"></a>自定义配置类向容器中注入组件(SpringBoot推荐)</h3><p>举个栗子：跟上面xml的例子一样。</p>
<p><strong>第一步，</strong> 跟上面一样，创建一个service</p>
<p><strong>第二步，</strong> 创建配置类：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.springboot.custom;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.springboot.service.UserService;</span><br><span class="line">import org.springframework.context.annotation.Bean;</span><br><span class="line">import org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line">@Configuration</span><br><span class="line">public class UserConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public UserService userService2() &#123;</span><br><span class="line">        return new UserService();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p><strong>需要注意的地方：</strong></p>
<ul>
<li>@Configuration 用于标识当前类是一个配置类，用来表示对应spring配置文件</li>
<li>@Bean 标识的方法用于向容器注入组件</li>
<li>方法的返回值就是注入容器中的组件对象</li>
<li>方法名是这个组件对象的 id值</li>
</ul>
<p><strong>第三步，</strong> 就是使用了，跟上面第四步一样。不需要上面的第三步。</p>
<h3 id="Profile多环境支持"><a href="#Profile多环境支持" class="headerlink" title="Profile多环境支持"></a>Profile多环境支持</h3><p>&emsp;&emsp;profile是Sping用来针对不同的环境要求，提供不同的配置支持。什么不同的环境呢？比方说：开发环境，生产环境。</p>
<p>&emsp;&emsp;全局 Profile 配置使用的文件名可以是<br>application-{profile}.properties / application-{profile}.yml 。如：application-dev.properties / application-prod.properties</p>
<p><strong>举个栗子：</strong></p>
<p>&emsp;&emsp;我们的项目环境分为 开发 （dev）和 生产（prod）环境 ，开发环境下端口号为 8081，<br>生产环境下端口号为8082。</p>
<h4 id="通过properties文件指定"><a href="#通过properties文件指定" class="headerlink" title="通过properties文件指定"></a>通过properties文件指定</h4><p><img src="/uploads/article/2019-08-23/profile-properties.png" alt="profile-properties.png"></p>
<p>&emsp;&emsp;如上图，创建了两个文件：application-dev.properties，application-prod.properties，里面内容很简单，就是指定端口号。server.port=8081</p>
<p>&emsp;&emsp;然后，我们在application.properties文件中指定激活哪一个文件即可。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">//激活application-dev.properties配置</span><br><span class="line">spring.profiles.active=dev</span><br><span class="line"></span><br><span class="line">//激活application-prod.properties配置</span><br><span class="line">spring.profiles.active=prod</span><br></pre></td></tr></table></figure></div>
<h4 id="通过yml文件指定"><a href="#通过yml文件指定" class="headerlink" title="通过yml文件指定"></a>通过yml文件指定</h4><p>&emsp;&emsp;不用新建文件，直接在application.yml写就可以了。</p>
<p><strong>PS:需要用三个减号隔开，表示不同的文档块。</strong></p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  profiles:</span><br><span class="line">    active: prod #激活哪个profile , 当前激活的是 prod 开发环境</span><br><span class="line"></span><br><span class="line">---</span><br><span class="line">server:</span><br><span class="line">  port: 8081</span><br><span class="line"></span><br><span class="line">spring:</span><br><span class="line">  profiles: dev #指定属于哪个环境, dev 环境时使用</span><br><span class="line"></span><br><span class="line">---</span><br><span class="line"></span><br><span class="line">server:</span><br><span class="line">  port: 8081</span><br><span class="line"></span><br><span class="line">spring:</span><br><span class="line">  profiles: prod #指定属于哪个环境, prod 环境时使用</span><br></pre></td></tr></table></figure></div>
      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2019/08/27/SpringBoot系列—Thymeleaf（五）/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            SpringBoot系列—Thymeleaf（五）
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2019/08/22/SpringBoot系列—-SpringBootApplication源码解析（三）/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">SpringBoot系列—@SpringBootApplication源码解析（三）</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>